#! /bin/bash
#
# Synopsis:
# This script "pings" each of the domains, sending the results
# to outfile.  The outfile is a list of lines with date and results
#
# The lines that start with "+" are positive result pings.  The
# rest of the lines are informational.
#

##################################################
# Do not change any of the following lines
##################################################

function expandfilename() {

# Input is $1
# output is
#    xpath is the pathname
#    xbase is the filename with extention
#    xfext is only the extention
#    xpref is only the filename
#
a=${1}
xpath=${a%/*}
 xbase=${a##*/}
 xfext=${xbase##*.}
 xpref=${xbase%.*}
}

#------------------------------------------------------------------------------

function testdownloadspeed() {
# This function will test the download speed.
# Command to use to test download:  $downloadspeedtest
# Output is grepped for "saved"

        ${1} 2> ${temp}
        speed=`grep ' saved ' ${temp} | tr '[:cntrl:]' ' ' | tr -d '()' | cut -d" " -f 3-4`
        fromfile=`grep "http" ${temp} | tr '[:cntrl:]' ' ' | cut -d " " -f 4`
        return="${fromfile} ${speed}"

}



#------------------------------------------------------------------------------


function formatresult {
# This function takes $1 -- the retults of a valid ping --
#    and formats it as follows:
# <hostname> <ip address> <ping time in ms>
#
# input string looks something like
#########################
#root@host:~# ping -c 1 google.com
#PING google.com (173.194.43.7) 56(84) bytes of data.
#64 bytes from lga15s34-in-f7.1e100.net (173.194.43.7): icmp_req=1 ttl=55 time=23.3 ms


#
#--- google.com ping statistics ---
#1 packets transmitted, 1 received, 0% packet loss, time 0ms
#rtt min/avg/max/mdev = 23.315/23.315/23.315/0.000 ms
#root@host:~#
#########################

return=""

# Find the two appropriate lines...
hostname=`echo $1 | cut -d" " -f 2`
ipaddress=`echo $1 | cut -d" " -f 3`
#pingtime=`echo $1 | cut -d" " -f 15 | cut -d"=" -f 2 | printf "%.1f"`
pingtime=`echo $1 | cut -d" " -f 15 | cut -d"=" -f 2 `
return="${hostname} ${ipaddress} ${pingtime}"


}



#------------------------------------------------------------------------------


function ispingokay {
        # This function will search the ping output text
        # in variable $1 and return either True 1 (ping
        # is okay) or False 0 (ping failed)
        #

        searchforstring="1 received"

        test=`echo "$1" | grep "${searchforstring}"`


        if [ -z "${test}" ]
        then
                # Problem... input string does not contain
                # search for string.  This means the ping failed
                return="${negativeresult}${datestr} ${1}"
        else
                # no problem!  The input string does contain
                # a line that matches searchforstring.  Now,
                # return the hostname & the ping time
                formatresult "$1"
                return="${positiveresult}${datestr} ${return}"

        fi
}

#######################################################################################
### configuration area
#######################################################################################

# Command to test download speed...
downloadspeedtest="wget -O /dev/null http://speedtest.wdc01.softlayer.com/downloads/test10.zip"

# Temporary file
temp="tempfile.log"





positiveresult="+"
negativeresult=" "

#outFile="/pinger/pinger.log"
#hostnameFile="/pinger/pinger.domains"

expandfilename $0

outFile="${xpath}/${xpref}.log"
hostnameFile="${xpath}/${xpref}.domains"

#######################################################################################
###  Main program
#######################################################################################

# Configuration:
# outFile is the name of the log file
# hostnameFile is the list of the domains to test
# postiveresult is the header to include on each positive result line
#

#outFile="`echo ${0}. | cut -d'.' -f1`.log" # Want to have the filename up until first dot



echo "0 = $0"
echo "outFile = $outFile"
echo "hostnameFile = $hostnameFile"


# Redirect all stdout stderr to a file
#exec >> "$outFile" 2>&1

#Capture all domain names
hostNames=`cat $hostnameFile`

echo "#                  `date`"

#for hName in "$hostNames"
while read hName
do
#       echo " >> $hName"
        # Only perform steps on a line that doesn't start with #
        if [[ $hName != \#* ]]
        then
                datestr=`date "+%Y-%m-%d:%H:%M:%S"`
                str=""
                str=`ping -c 1 ${hName}`
                if [ "$str"  = "" ] ; then
                        echo "ERROR: Null string returned from ping"
                        # We have an issue!  Ping returned nothing.














                        pingstr=" ISSUE ping ${hName} returned nothing"

                else
                        # ping returned something... now strip it of control characters
                        pingstr=`echo "${str}" | tr "[:cntrl:]" " "`
                        ispingokay "${pingstr}"
                        pingstr="${return}"
                fi


#echo "ping string is #${pingstr}#"

                echo "${pingstr}" | tee -a  ${outFile}
        fi
        # else, a comment line
#done
done < $hostnameFile

#testdownloadspeed ${downloadspeedtest}
#echo ${return} | tee -a ${outFile}


exit 0

